var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'',
    client_abbre:'',
    client_name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options){
    this.setData({
      type:options.type, //apply 创建门店并申请合作
      id:options.id
    });
  },
  username_input(e){
    this.setData({
      username:e.detail.value
    });
  },
  username_del(){
    var that = this;
    setTimeout(function (){
      that.setData({
        username:''
      })
    },100);
  },
  client_abbre_input(e){
    this.setData({
      client_abbre:e.detail.value
    });
  },
  client_abbre_del(){
    var that = this;
    setTimeout(function (){
      that.setData({
        client_abbre:''
      })
    },100);
  },
  client_name_input(e){
    this.setData({
      client_name:e.detail.value
    });
  },
  client_name_del(){
    var that = this;
    setTimeout(function (){
      that.setData({
        client_name:''
      })
    },100);
  },
  // 确定创建账号
  confirm_create(){
    var that = this;
    if(that.data.username==''){
      wx.showLoading({
        title:'请填写姓名',
        duration:500
      });
    }else if(that.data.client_abbre==''){
      wx.showLoading({
        title:'请填写账号简称',
        duration:500
      });
    }else{
      wx.showModal({
        title: '提示',
        content: '确认创建账号',
        success: function (res) {
          if (res.confirm) {
            app.AppHttp('api/Client/CreateClient',{
              openid: app.globalData.openid,
              client_abbre:that.data.client_abbre,
              client_name:that.data.client_abbre,
              username:that.data.username,
              account_number:''
            }, function (res) {
              if(res.data.status == 1){
                if(that.data.type=='apply'){
                  app.AppHttp("api/Client/basicInfo", { openid: app.globalData.openid }, function (res) {
                    that.conf_AddsuperOk_perf(res.data.id);
                  });
                }else{
                  wx.reLaunch({
                    url:'/group_space/pages/success_yes/success_yes'
                  }); 
                };
              }else{
                wx.showToast({
                  title:res.data.msg
                });    
              };
            });
          }
        }
      });
    }
  },
  // 提交申请合作
  conf_AddsuperOk_perf(client_id){
    var that = this;
    var message_type;
    app.AppHttp("api/Client/getSymbiosis", {supplier_id:that.data.id,client_id:client_id}, function (res) {
      let sao_super = res.data.data.supplier_info;
      if (sao_super.level == 1) {  //合作供应商端
        message_type = 'add_info';
        that.AddSupplier_fun(client_id,message_type);
      } else if (sao_super.level == 4) { //合作批发端
        message_type = 'pf_add_info';
        app.AppHttp('api/Supplier/getCoopCustomCount', {  //免费版限制客户
          supplier_id:that.data.id
        }, function (res) {
          if(res.data.supplier_identity==0 && res.data.custom_count>=10){
            wx.showModal({
              title: '提示',
              content: "该供应商已有"+res.data.custom_count+"个客户\n供应商版本限定\n客户总量最多10个",
              showCancel: false,
              confirmText:"我知道了",
              success: function (res) {
                app.globalData.CreateSuper=1;
                wx.switchTab({
                  url: '/pages/shangpin/shangpin'
                });
              }
            });
          }else{
            that.AddSupplier_fun(client_id,message_type);
          };
        });
      };
    }); 
  },
  // 添加供应商接口
  AddSupplier_fun(client_id,message_type){
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/Client/AddSupplier',
      method: "POST",
      data: {
        supplier_id: that.data.id,
        openid: app.globalData.openid
      },
      success: function (res) {
        app.globalData.CreateSuper=1;
        wx.showToast({
          title: "申请成功",
        }, 800);
        // 向供应商发送申请通知
        wx.switchTab({
          url: '/pages/shangpin/shangpin'
        });
        setTimeout(function (){
          wx.request({
            url: app.globalData.url + 'api/Message1/add_info',
            method: 'GET',
            data: {
              client_id: client_id,
              supplier_id: that.data.id,
              type: 1,   //1为请求，2为同意
              message_type: message_type
            },
            success: function (res) {
              if (sao_super.is_auto_agree == 1) {
                wx.request({
                  url: app.globalData.url + 'api/Message1/add_info',
                  method: 'GET',
                  data: {
                    client_id: client_id,
                    supplier_id:  that.data.id,
                    type: 2,  //1为请求，2为同意
                    message_type: message_type
                  },
                  success: function (res) {}
                });
              };
            }
          });
        },800);     
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(){

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide(){

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload(){

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh(){

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){

  }
})